const configService = require('../services/configService');
const { success, successWithData, error } = require('../utils/resultwrapper');

class ConfigController {
    /**
     * 创建系统配置
     */
    async createConfig(req, res) {
        try {
            const configData = req.body;
            const result = await configService.createConfig(configData);
            res.json(successWithData(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }

    /**
     * 更新系统配置
     */
    async updateConfig(req, res) {
        try {
            const { id } = req.params;
            const configData = req.body;
            const result = await configService.updateConfig(id, configData);
            res.json(successWithData(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }

    /**
     * 删除系统配置
     */
    async deleteConfig(req, res) {
        try {
            const { id } = req.params;
            const result = await configService.deleteConfig(id);
            res.json(success(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }

    /**
     * 获取系统配置详情
     */
    async getConfigById(req, res) {
        try {
            const { id } = req.params;
            const result = await configService.getConfigById(id);
            res.json(successWithData(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }

    /**
     * 获取系统配置列表
     */
    async getConfigList(req, res) {
        try {
            const result = await configService.getConfigList(req.query);
            res.json(successWithData(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }
    /**
     * 获取所有系统配置
     */
    async getAllConfigs(req, res) {
        try {
            const result = await configService.getAllConfig(req.query);
            res.json(successWithData(result));
        } catch (err) {
            res.json(error(err.message));
        }
    }
}

module.exports = new ConfigController();